'use strict';
/**
 * @fileoverview VIP
 * @author sundbcn@gmail.com (sun debing)
 */

var mongoose = require('mongoose');
var moment = require('moment');
var _ = require('lodash');
var constantUtil = require('../util/constantUtil');
var unionGroupMgr = require('../mgr/unionGroupMgr.js');
var dataApi = require('../libs/dataApi');

/**
 * 联盟帮助功能
 */
var unionHelpSchema = new mongoose.Schema({
    // 联盟id
    union_id: {
        type: Number,
        required: true,
        index: true
    },

    // 角色id
    role_id: {
        type: Number,
        required: true,
        index: true
    },

    // 共用id
    job: {
        type: Number,
        default: 0
    },

    // 共用id
    name: {
        type: String,
    },

    // 共用id
    share_id: {
        type: Number,
        default: 0
    },

    //结束时间
    end_time: {
        type: Number,
        default: 0
    },

    // 帮助类型
    help_type: {
        type: Number,
        default: 0
    },

    // 等级
    lv: {
        type: Number,
        default: 0
    },

    // 已帮助列表
    //help_list: [Number],

    // 帮助剩余次数
    help_max_num: {
        type: Number,
        default: 0
    },

    // 帮助最大次数
    help_max_times: {
        type: Number,
        default: 0
    }
});

unionHelpSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 增加帮助任务
 * @return {[type]} [description]
 */
unionHelpSchema.methods.getUnion = function () {

};

/**
 * 获取修改
 * @return {[type]} [description]
 */
unionHelpSchema.methods.getLastChange = function () {
    return this.toJSON();
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
unionHelpSchema.methods.push = function () {
    this.pushModify();
};

unionHelpSchema.methods.pushModify = function () {
    unionGroupMgr.sendUnionHelpChangedOnly(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send vip message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!unionHelpSchema.options.toJSON) {
    unionHelpSchema.options.toJSON = {};
}
/* jshint unused:false */
unionHelpSchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Help', unionHelpSchema);
